package com.eg.xmlhandler;

import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.eg.mp3file.Mp3FileInfo;

public class Mp3XMLHandler extends DefaultHandler {
	private List<Mp3FileInfo> infos = null;
	private Mp3FileInfo info = null;
	private String tagName = null;
	
	public Mp3XMLHandler(List<Mp3FileInfo> infos) {
		this.infos = infos;
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		tagName = qName;
		if (qName.equals("resource")){
			info = new Mp3FileInfo();
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if (qName.equals("resource")){
			infos.add(info);
		}
		tagName = "";
	}
	
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		String t = new String (ch, start, length);
		
		if (tagName.equals("id")){
			info.setRec_NO(t);
		}else if (tagName.equals("mp3.name")){
			info.setMp3_name(t);
		}else if (tagName.equals("mp3.size")){
			info.setMp3_size(t);
		}else if (tagName.equals("lrc.name")){
			info.setLrc_name(t);
		}else if (tagName.equals("lrc.size")){
			info.setLrc_size(t);
		}
	}

	@Override
	public void startDocument() throws SAXException {}
	
	@Override
	public void endDocument() throws SAXException {}

}
